package org.springcloud.wisdom.amusement.service.serviceimp;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springcloud.wisdom.amusement.dao.StudentDao2;
import org.springcloud.wisdom.amusement.dao.Team_StudentDao;
import org.springcloud.wisdom.amusement.entity.Student;
import org.springcloud.wisdom.amusement.entity.Team_Student;
import org.springcloud.wisdom.amusement.service.ITeam_StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
public class Team_StudentServiceImp implements ITeam_StudentService {
    @Autowired
    Team_StudentDao teamStudentDao;
    @Autowired
    StudentDao2 studentDao;
    @Override
    public List<Student> watchTeamStudent(Integer teamid) {
        if (teamid == null) return null;
        return teamStudentDao.selectStudent(teamid);
    }

    @Override
    public IPage<Student> watchTeamStudentPage(int currentPage, int size, Integer teamid) {
        Page<Student> pageRequest = new Page<>(currentPage, size);
        IPage<Student> watchTeamStudent = teamStudentDao.selectStudentPage(pageRequest,teamid);
        return watchTeamStudent;
    }

    @Override
    public IPage<Student> watchAddTeamStudentPage(int currentPage, int size, Integer teamid) {
        Page<Student> pageRequest = new Page<>(currentPage, size);
        IPage<Student> watchTeamStudent = teamStudentDao.selectAddStudentPage(pageRequest,teamid);
        return watchTeamStudent;
    }

    @Override
    public List<Student> watchAddTeamStudent(Integer teamid) {
        if (teamid == null) return null;
        return teamStudentDao.selectAddStudent(teamid);
    }

    @Override
    public Boolean wantAddTeamStudent(Team_Student teamStudent) {
        if (teamStudent.getSid().trim().length()==0 || teamStudent.getSid()==null){
            return false;
        }
        if (teamStudent.getTeamid() == null){
            return  false;
        }
        Student student = new Student();
        String sid = teamStudent.getSid();
        String talent = teamStudent.getReason();
        student.setSid(sid);
        student.setTalent(talent);
        studentDao.update(student);
        return teamStudentDao.addPlayer(teamStudent) > 0;
    }

    @Override
    public Boolean delTeamStudent(Team_Student teamStudent) {
        return teamStudentDao.delPlayer(teamStudent) > 0;
    }

    @Override
    public Boolean permitAddTeamStudent(Team_Student teamStudent) {
        return teamStudentDao.realAddPlayer(teamStudent) > 0;
    }

    @Override
    public Boolean unpermitAddTeamStudent(String sid, Integer teamid) {
        if (sid.trim().length()==0 || sid==null){
            return false;
        }
        if (teamid == null){
            return  false;
        }
        return teamStudentDao.deleteAddPlayer(sid, teamid) > 0;
    }
}
